package com.jincp.common.http;

import java.io.File;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Observer;
import java.util.concurrent.ConcurrentHashMap;

import okhttp3.Headers;

/**
 * =========================================================
 * @ProjectName: 网络请求参数
 * @ClassName: HttpRequestParams
 * @Description:
 * @Author: jincp
 * @CreateDate: 2019/9/3 11:52
 * =========================================================
 */
public class HttpRequestParams {

    private final ConcurrentHashMap<String, String> mHeaderMap = new ConcurrentHashMap<>();

    private final ConcurrentHashMap<String, String> mUrlParams = new ConcurrentHashMap<>();

    private final ConcurrentHashMap<String, File> mFileParams = new ConcurrentHashMap<>();

    private final ConcurrentHashMap<String, Object> mJsonParams = new ConcurrentHashMap<>();

    public void putHeader(String key, int value) {
        putHeader(key, String.valueOf(value));
    }

    public void putHeader(String key, float value) {
        putHeader(key, String.valueOf(value));
    }

    public void putHeader(String key, double value) {
        putHeader(key, String.valueOf(value));
    }

    public void putHeader(String key, boolean value) {
        putHeader(key, String.valueOf(value));
    }

    public void putHeader(String key, String value) {
        if (key != null && value != null) {
            mHeaderMap.put(key, value);
        }
    }

    public void putHeader(Map<String, String> params) {
        if (params != null && params.size() > 0) {
            mHeaderMap.putAll(params);
        }
    }

    public void put(String key, long value) {
        put(key, String.valueOf(value));
    }

    public void put(String key, double value) {
        put(key, String.valueOf(value));
    }

    public void put(String key, boolean value) {
        put(key, String.valueOf(value));
    }

    public void put(String key, String value) {
        if (key != null && value != null) {
            mUrlParams.put(key, value);
        }
    }

    public void put(Map<String, String> params) {
        if (params != null && params.size() > 0) {
            mUrlParams.putAll(params);
        }
    }

    public void putFile(String key, File file) {
        if (key != null && file != null) {
            mFileParams.put(key, file);
        }
    }

    public void putJson(String key, String value) {
        if (key != null && value != null) {
            mJsonParams.put(key, value);
        }
    }

    public void putJson(String key, Object value) {
        if (key != null && value != null) {
            mJsonParams.put(key, value);
        }
    }

    public Map<String, String> getHeaderParams() {
        return mHeaderMap;
    }

    public Map<String, String> getUrlParams() {
        return mUrlParams;
    }

    public Map<String, Object> getJsonParams() {
        return mJsonParams;
    }

    public Map<String, File> getFileParams() {
        return mFileParams;
    }


    public void remove(String key) {
        mHeaderMap.remove(key);
        mUrlParams.remove(key);
        mFileParams.remove(key);
        mJsonParams.remove(key);
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        for (ConcurrentHashMap.Entry<String, String> entry : mHeaderMap.entrySet()) {
            result.append("Header");
            if (result.length() > 0)
                result.append("&");

            result.append(entry.getKey());
            result.append("=");
            result.append(entry.getValue());
        }
        result.append("Params");
        for (ConcurrentHashMap.Entry<String, String> entry : mUrlParams.entrySet()) {
            if (result.length() > 0)
                result.append("&");

            result.append(entry.getKey());
            result.append("=");
            result.append(entry.getValue());
        }
        for (ConcurrentHashMap.Entry<String, File> entry : mFileParams.entrySet()) {
            if (result.length() > 0)
                result.append("&");

            result.append(entry.getKey());
            result.append("=");
            result.append("FILE");
            result.append(entry.getValue().getAbsoluteFile());
        }
        for (ConcurrentHashMap.Entry<String, Object> entry : mJsonParams.entrySet()) {
            if (result.length() > 0)
                result.append("&");

            result.append(entry.getKey());
            result.append("=");
            result.append("JSON");
            result.append(entry.getValue().toString());
        }
        return result.toString();
    }

    public String getUrlParamString() {
        StringBuilder result = new StringBuilder();
        for (ConcurrentHashMap.Entry<String, String> entry : mUrlParams.entrySet()) {
            if (result.length() > 0)
                result.append("&");
            try {
                result.append(entry.getKey());
                result.append("=");
                result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
            } catch (Throwable e) {
                return "";
            }
        }
        return result.toString();
    }
}
